package com.example.himalaya.fragments;

import android.content.Intent;
import android.graphics.Rect;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.adapters.RecommendListAdapter;
import com.example.himalaya.base.BaseFragment;
import com.example.himalaya.interfaces.IRecommendViewCallback;
import com.example.himalaya.interfaces.OnRecommendItemClickListener;
import com.example.himalaya.interfaces.OnRetryClickLisener;
import com.example.himalaya.presenters.AlbumDetialPresenter;
import com.example.himalaya.presenters.RecommendPresenter;
import com.example.himalaya.ui.DetailActivity;
import com.example.himalaya.utils.ButtonUtils;
import com.example.himalaya.utils.LogUtils;
import com.example.himalaya.views.UILoader;
import com.ximalaya.ting.android.opensdk.model.album.Album;

import net.lucode.hackware.magicindicator.buildins.UIUtil;

import java.util.List;

/**
 * @Author Li Yue
 * @Date 2021/3/22
 **/
public class RecommendFragment extends BaseFragment implements IRecommendViewCallback, OnRetryClickLisener, OnRecommendItemClickListener {
    private String TAG = "RecommendFragment";
    private View rootView;
    private UILoader uiLoader;
    private RecyclerView recommendView;
    private RecommendListAdapter recommendListAdapter;
    private RecommendPresenter mRecommendPresenter;

    @Override
    protected View onSubViewLoaded(final LayoutInflater layoutInflater, final ViewGroup container) {

        uiLoader = new UILoader(getContext()) {
            @Override
            protected View getSuccessView(ViewGroup container) {
                return createSuccessView(layoutInflater, container);
            }
        };

/**
 //拿数据回来
 getRecommend();
 **/
        //获取到逻辑层的对象
        mRecommendPresenter = RecommendPresenter.getsInstance();
        //先设置通知接口的注册
        mRecommendPresenter.registerViewCallback(this);
        mRecommendPresenter.getRecommendList();

        if (uiLoader.getParent() instanceof ViewGroup) {
            ((ViewGroup) uiLoader.getParent()).removeView(uiLoader);
        }
        uiLoader.setOnRetryClickListener(this);

        //返回View
        return uiLoader;
    }

    private View createSuccessView(LayoutInflater layoutInflater, ViewGroup container) {
        //VIew加载完成
        rootView = layoutInflater.inflate(R.layout.fragment_recommed, container, false);
        //RecyclerView的使用
        //1.找到控件
        recommendView = rootView.findViewById(R.id.recommend_list);
        //2.设置布局管理器
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recommendView.setLayoutManager(linearLayoutManager);
        recommendView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
                outRect.left = UIUtil.dip2px(view.getContext(), 10);
                outRect.right = UIUtil.dip2px(view.getContext(), 10);
                outRect.bottom = UIUtil.dip2px(view.getContext(), 10);
                // Add top margin only for the first item to avoid double space between items
                if (parent.getChildPosition(view) == 0)
                    outRect.top = UIUtil.dip2px(view.getContext(), 10);

            }
        });

        //3.设置适配器
        recommendListAdapter = new RecommendListAdapter();
        recommendView.setAdapter(recommendListAdapter);
        recommendListAdapter.setOnRecommendItemClickListener(this);
        return rootView;
    }


    @Override
    public void onRecommentListLoaded(List<Album> result) {
        //当获取推荐内容成功后调用该方法
        //数据回来以后 就是更新UI
        //把数据设置给适配器并更新
        recommendListAdapter.setData(result);
        uiLoader.updateStatus(UILoader.UIStatus.SUCCESS);
    }

    @Override
    public void onNetworkError() {
        LogUtils.d(TAG, "networkerror***********************************");
        uiLoader.updateStatus(UILoader.UIStatus.NETWORK_ERROR);
    }

    @Override
    public void onEmpty() {
        LogUtils.d(TAG, "empty*************************************");
        uiLoader.updateStatus(UILoader.UIStatus.EMPTY);
    }

    @Override
    public void onLoading() {
        LogUtils.d(TAG, "Loading******************************************");
        uiLoader.updateStatus(UILoader.UIStatus.LOADING);
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        //取消接口的注册，防止内存泄漏（不再用但是还有人持有，造成内存不够用）
        if (mRecommendPresenter != null) {
            mRecommendPresenter.unRegisterViewCallback(this);
        }
    }

    @Override
    public void onRetryClick() {
        //表示网络不佳的时候用户点击重试
        //重新获取数据
        if (mRecommendPresenter != null) {
            mRecommendPresenter.getRecommendList();
        }

    }

    @Override
    public void OnItemClick(int position, Album album) {
        //条目点击事件,跳转详情界面
        if (!ButtonUtils.isFastDoubleClick()) {
            AlbumDetialPresenter.getsInstance().setTargetAlbum(album);
            Intent intent = new Intent(getContext(), DetailActivity.class);
            startActivity(intent);

        }
    }
}
